package parts3.lesson37;

/**
 * 模拟一个死锁,
 * 测试结果: 有概率会死锁
 */
class Pen {
    public synchronized void get(Book book) {
        System.out.println("为了得到书");
        book.res();
    }

    public synchronized void res() {
        System.out.println("你先给我书，我就给你笔");
    }
}

class Book {
    public synchronized void get(Pen pen) {
        System.out.println("为了得到笔");
        pen.res();
    }

    public synchronized void res() {
        System.out.println("你先给我笔，我就给你书");
    }
}

public class DeadLock implements Runnable {
    private static Book book = new Book();
    private static Pen pen = new Pen();

    public DeadLock() {
        new Thread(this).start();
        pen.get(book);
    }

    public static void main(String[] args) {
        new DeadLock();

    }

    @Override
    public void run() {
        book.get(pen);
    }
}
